import spock.lang.Specification

/**
 * Created at 2017/11/2 18:23
 * @author gongweixin
 */
class ListExample extends Specification {
    def "test list sum"() {
        expect:
        [1, 2].sum() == 3
        [].sum() == null
        [Double.NaN, 1D].sum() == Double.NaN
    }

    def "test list flatten, 里层list打平到外层"() {
        expect:
        [1,2,3,4] == [1,2,[3,4]].flatten()
        [1,2,null] == [1,2,[null]].flatten()
        [1,2,3,null] == [1: [1], 2: [2,3], 3: null].values().flatten()
        [null,1,2,3] == [3: null, 1: [1], 2: [2,3]].values().flatten()
    }

    def "test list null group, list中有null进行group" () {
        when:
        [1: [[key:1]], 2: [[key:1]]] == [[key:1],[key:2],null].groupBy {it.key}
        then:
        thrown(NullPointerException)
    }

    def "test list get null"() {
        expect:
        [null, null] == [[a:1],[b:2]].c
    }

    def "test list unique"() {
        expect:
        [[a:1]] == [[a:1],[a:1]].unique()
        [[a:1, b:"2"]] == [[a:1, b:"2"],[a:1, b:"2"]].unique()
    }

   def "test sub list"() {
       expect:
       [1,2,3] == [1,2,3,4,5][0..2]
   }

    def "test max list"() {
        expect:
        [value:2] == [[value:1], [value:2]].max {it.value}
        null == [].max()
    }

    def "test intersect list"() {
        expect:
        [2] == [1,2].intersect([2,3])
        [2] == [1,2,2].intersect([2,3])
    }

    def "test def more"() {
        expect:
        def (a,b) = "a:b".split(":")
        a == 'a'
        b == 'b'
    }
}
